package com.blame.springblog.common.interceptor;


import com.blame.springblog.common.utils.JwtUtils;
import com.blame.springblog.constant.Constants;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;


@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userToken=request.getHeader(Constants.USER_TOKEN_HEADER_KEY);
        if(userToken==null){
            response.setStatus(401);
            return false;
        }
        Claims claims= JwtUtils.parseToken(userToken);
        if(claims==null){
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
